
import argparse
import yaml

def parse_arguments(config_file=None):
    parser = argparse.ArgumentParser(description="3DTilesVPR",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--config", type=str)
    args = parser.parse_args()

    class dict2obj(object):
        def __init__(self, d):
            for a, b in d.items():
                if isinstance(b, (list, tuple)):
                    setattr(self, a, [dict2obj(x) if isinstance(x, dict) else x for x in b])
                else:
                    setattr(self, a, dict2obj(b) if isinstance(b, dict) else b)
    if config_file is not None:
        args.config = config_file
    
    config = None
    with open(args.config, "r") as f:
        config = yaml.safe_load(f)
        args2 =  dict2obj(config)
        # t_args = argparse.Namespace(*config)
        # t_args.__dict__.update(config)
        args = parser.parse_args(namespace=args2)
    return args,config